package com.fenshi.manager.activity.user;

import android.app.ProgressDialog;
import android.location.Location;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdate;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.model.CameraPosition;
import com.amap.api.maps2d.model.LatLng;
import com.fenshi.manager.R;
import com.fenshi.presenter.imp.ParkLocationPresenterImp;
import com.fenshi.view.LocationView;
import com.quickdv.activity.MapActivity;
import com.quickdv.until.AlertHelper;

public class LocationActivity extends MapActivity implements LocationSource,AMapLocationListener,LocationView {


    private LocationManagerProxy mLocationManagerProxy;
    private OnLocationChangedListener mListener;
    private LatLng latlng;
    private ProgressDialog load;
    private ParkLocationPresenterImp presenter;

    @Override
    public int getLayoutID() {
        return R.layout.activity_location;
    }

    @Override
    public void initView() {
        mapView = findView(R.id.map);

    }

    @Override
    public void initActionbar() {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_location,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_save:
                presenter.saveLocation();
                break;

        }
        return true;
    }

    @Override
    public void initData() {
        load = new AlertHelper(this).LoadingAlert();
        presenter = new ParkLocationPresenterImp(this);
    }

    protected void init() {
        super.init();
        setUpMap();
    }

    private void setUpMap() {
        aMap.setLocationSource(this);// 设置定位监听
        aMap.getUiSettings().setZoomControlsEnabled(false);
        aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
        aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false
        // 设置定位的类型为定位模式：定位（AMap.LOCATION_TYPE_LOCATE）、跟随（AMap.LOCATION_TYPE_MAP_FOLLOW）
        // 地图根据面向方向旋转（AMap.LOCATION_TYPE_MAP_ROTATE）三种模式
//        aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);

    }

    /**
     * 激活定位
     */
    @Override
    public void activate(LocationSource.OnLocationChangedListener listener) {
        mListener = listener;
        if (mLocationManagerProxy == null) {
            mLocationManagerProxy = LocationManagerProxy.getInstance(this);
            //此方法为每隔固定时间会发起一次定位请求，为了减少电量消耗或网络流量消耗，
            //注意设置合适的定位时间的间隔，并且在合适时间调用removeUpdates()方法来取消定位请求
            //在定位结束后，在合适的生命周期调用destroy()方法
            //其中如果间隔时间为-1，则定位只定一次
            mLocationManagerProxy.requestLocationData(
                    LocationProviderProxy.AMapNetwork, -1, 500, this);
        }
    }

    /**
     * 停止定位
     */
    @Override
    public void deactivate() {
        mListener = null;
        if (mLocationManagerProxy != null) {
            mLocationManagerProxy.removeUpdates(this);
            mLocationManagerProxy.destroy();
        }
        mLocationManagerProxy = null;
    }

    @Override
    public void onLocationChanged(Location location) {

    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }

    private void changeCamera(CameraUpdate update, AMap.CancelableCallback callback) {
        //初始化地图中心位置
        aMap.animateCamera(update, 1000, callback);
    }

    @Override
    public void onLocationChanged(AMapLocation amapLocation) {
        log.v("MainFragment","this is loactionChagaed");
        if (amapLocation != null) {
            //获取位置信息
            Double geoLat = amapLocation.getLatitude();
            Double geoLng = amapLocation.getLongitude();
            latlng =new LatLng(geoLat, geoLng);
            changeCamera(
                    CameraUpdateFactory.newCameraPosition(new CameraPosition(
                            latlng, 18, 0, 30)), null);
            mListener.onLocationChanged(amapLocation);
            deactivate();
        }

    }

    @Override
    public void showSubmit() {
        if (load!=null  && !load.isShowing())
            load.show();
    }

    @Override
    public void dismissSubmit() {
        if (load!=null  && load.isShowing())
            load.dismiss();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void dismissLoading() {

    }

    @Override
    public void showInfo(String info) {
        Toast.makeText(this, info,Toast.LENGTH_LONG).show();
    }

    @Override
    public LatLng getLocation() {
        return latlng;
    }
}
